<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/9/18
 * Time: 18:27
 */
namespace frontend\controllers;
use frontend\models\Cart;
use frontend\models\Goods;
use yii;
use yii\helpers\ArrayHelper;
class CartController extends BaseController{

    /**
     * 购物车详情
     */
    public function actionInfo(){
        /*商品列表*/
        /*总金额*/
        /*
        left join是以A表的记录为基础的,A可以看成左表,B可以看成右表,left join是以左表为准的.
        换句话说,左表(A)的记录将会全部表示出来,而右表(B)只会显示符合搜索条件的记录(例子中为: A.aID = B.bID).
        */
        $c_lists = Cart::find()
            ->select(['c.id', 'g.name', 'g.price', 'g.image_id', 'g.unit', 'c.num'])
            ->from('cart as c')
            ->innerJoin('goods as g', 'g.id = c.gid')
            ->where(['g.status' => 1, 'c.uid' => yii::$app->user->getId() | 1])
            ->asArray()
            ->all();
        /**
         * 属略图
         */
        foreach ($c_lists as $key => $value){
            $c_lists[$key]['smail_img'] = Yii::$app->imagemanager->getImagePath($value['image_id'], '100px', '85px',true);
        }

        //$c_lists = yii\helpers\Json::encode($c_lists);

        return $this->render('info', ['c_lists' => $c_lists]);
    }

    public function actionSum(){
        $sum = Cart::find()
            ->select(["SUM(g.price)"])
            ->from('cart as c')
            ->innerJoin('goods as g', 'g.id = c.gid')
            ->where(['g.status' => 1, 'c.uid' => yii::$app->user->getId() | 1])
            ->asArray()
            ->one();
        echo "sum(".array_values($sum)[0].")";
    }

    public function actionAdd(){
        $gid = yii::$app->request->get('gid');
        $num = yii::$app->request->get('num');

        if(! $gid && uid){
            return false;
        }

        $cart = Cart::find()->where(['gid' => $gid, 'uid'  => yii::$app->user->getId() | 1])->one();

        if($cart){
           /*已经在购物车*/
           $cart->setScenario('update');
           if($cart->addNum($num)->save()){
                return 'alert("添加成功")';
           }
        }else if(!yii::$app->user->isGuest){
            $cart = new Cart();
            $cart->setScenario('create');
            $cart->gid = $gid;
            $cart->num = $num;

            if($cart->save()){
                return 'alert("添加成功")';
            }
        }
        return 'alert("异常")';
    }

    public function actionDown(){
        $gid = yii::$app->request->get('gid');
        $num = yii::$app->request->get('num');

        if(! $gid && $num){
            return false;
        }

        $cart = Cart::find()->where(['gid' => $gid, 'uid'  => yii::$app->user->getId() | 1])->one();

        if($cart){
            /*已经在购物车*/
            $cart->setScenario('update');
            if($cart->downNum($num)->save()){
                return 'alert("更改成功")';
            }
        }
        return 'alert("异常")';
    }

    public function actionDel(){
        $gid = yii::$app->request->get('gid');

        $cart = Cart::find()->where(['gid' => $gid, 'uid'  => yii::$app->user->getId() | 1])->one();

        if($cart){
            if($cart->delete()){
                return "alert('成功!')";
            }
        }

        return "alert('异常')";
    }

    public function actionEmpty(){
        $user = yii::$app->user->getId();
        if($user){
            if(Cart::deleteAll(['uid' => $user])){
                return "alert('成功!')";
            }
        }
        return "alert('异常!')";
    }
}
